from django.contrib import admin

from . import models
from celery_tasks.html.tasks import generate_static_sku_detail_html, generate_static_list_search_html
# Register your models here.



class SKUDetailMixin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):

        obj.save()
        sku_id = obj.id
        generate_static_sku_detail_html.delay(sku_id)

class SKUImageDetailMiXin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):

        obj.save()
        sku_id = obj.sku.id
        generate_static_sku_detail_html.delay(sku_id)

    def delete_model(self, request, obj):

        sku_id = obj.sku.id
        obj.delete()
        generate_static_sku_detail_html.delay(sku_id)

class SKUSpecificationDetailMixin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):

        obj.save()
        sku_id = obj.sku.id
        generate_static_sku_detail_html.delay(sku_id)
        sku = obj.sku
        if not sku.default_image_url:
            sku.default_image_url = obj.image.url
            sku.save()

    def delete_model(self, request, obj):

        sku_id = obj.sku.id
        obj.delete()
        generate_static_sku_detail_html.delay(sku_id)
class GoodsCategoryMixin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):

        obj.save()
        generate_static_list_search_html.delay()


    def delete_model(self, request, obj):

        obj.delete()
        generate_static_list_search_html.delay()

admin.site.register(models.GoodsCategory, GoodsCategoryMixin)
admin.site.register(models.GoodsChannel)
admin.site.register(models.Goods)
admin.site.register(models.Brand)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU, SKUDetailMixin)
admin.site.register(models.SKUSpecification, SKUSpecificationDetailMixin)
admin.site.register(models.SKUImage, SKUImageDetailMiXin)